package org.kumas.mybatissimple.session.defaults;

import org.kumas.mybatissimple.binding.MapperMethod;
import org.kumas.mybatissimple.binding.MapperProxy;
import org.kumas.mybatissimple.executor.Executor;
import org.kumas.mybatissimple.session.Configuration;
import org.kumas.mybatissimple.session.SqlSession;

import java.lang.reflect.Proxy;
import java.sql.SQLException;

/*************************************************
 * TODO_Kumas
 *  @author KumasZhang
 *  DateTime： 2011-02-13 9:51
 *  Description： 
 **/
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;
    private Executor executor;

    public DefaultSqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }


    @Override
    public <T> T selectOne(MapperMethod mapperMethod, Object parameter) {
        try {
            return executor.query(mapperMethod,parameter);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public <T> T getMapper(Class<T> type) {
        return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type},new MapperProxy<>(this,type));
    }

    @Override
    public  Configuration getConfiguration(){
        return configuration;
    }
}
